package com.howells.time.module.login.presenter;


import com.howells.time.base.http.HttpURL;
import com.howells.time.base.http.HttpUtils;
import com.howells.time.base.http.JsonCallback;
import com.howells.time.module.login.view.LoginActivity;
import com.howells.time.module.login.view.LoginView;
import com.howells.time.module.main.model.User;
import com.orhanobut.logger.Logger;

import java.util.Map;

import okhttp3.Call;


/**
 * Created by Administrator on 2016.3.21.
 */
public class LoginPresenterImp implements LoginPresenter {

    private LoginView loginActivity;

    public LoginPresenterImp(LoginActivity loginActivity){
        this.loginActivity=loginActivity;
    }

    @Override
    public void login(final String username,final String password) {
        final User info=new User(username,password);
        if(username.equals("000")&&password.equals("000")){
            loginActivity.loginSuccess(info);
            return;
        }

        HttpUtils.PostObject(HttpURL.LoginApi.toString(), info, new JsonCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Logger.d(e);
                loginActivity.resultMsg("网络异常!");
            }

            @Override
            public void onResponse(Map<String, Object> response, int id) {
                if(response.get("message").equals("success")){
                    loginActivity.loginSuccess(info);
                }else {
                    Logger.e(response.toString());
                    loginActivity.resultMsg(response.get("message").toString());
                }
            }
        });

    }
}
